package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Login struct {
	// bingding:"required" 表示必传,不然报错
	User    string `form:"username" json:"user" uri:"user" xml:"user" bingding:"required"`
	Pasword string `form:"password" json:"password" uri:"password" xml:"password" bingding:"required"`
}

func main() {
	r := gin.Default()
	// json绑定
	r.POST("/:user/:password", func(context *gin.Context) {
		var login Login

		if err := context.ShouldBindUri(&login); err != nil {
			context.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
			return
		}
		// 用户名密码是否正确
		if login.User != "root" || login.Pasword != "123456" {
			context.JSON(http.StatusBadRequest, gin.H{"status": 304})
			return
		}
		context.JSON(http.StatusOK, gin.H{"status": 200})
	})

	r.Run(":8000")

	// 测试: curl http://127.0.0.1:8000/root/123456 -X POST
}
